<?php
	/**
	 * @package My QCubed Application
	 * @subpackage Drafts
	 *
	 */
	class SampleTitlesEditPanel extends QPanel {
		// Local instance of the Meta DataGrid to list SampleTitleses
                public $mctSampleTitles;
                public $objSample;

                public $txtaISBN;
                public $txtaTitle;
                public $txtNote;
                public $lblError;
                public $btnAdd;

		// Callback Method Names
		protected $strCloseEditPanelMethod;


                public function txtaISBN_Change($strParameter){
                    $objMemberArray = Title::QueryArray(
                                        QQ::Like(QQN::Title()->Isbn, $strParameter . '%')
                        );
                        $result = array();

                        foreach($objMemberArray as $objMember){
                            $result[] = $objMember->Isbn;
                        }
                        return $result;
                }
                public function txtaTitle_Change($strParameter){
                    $objMemberArray = Title::QueryArray(
                                        QQ::Like(QQN::Title()->Title, $strParameter . '%')

                        );
                        $result = array();

                        foreach($objMemberArray as $objMember){
                            $result[] = $objMember->Title;
                        }
                        return $result;
                }


		public function __construct($objParentObject,$strCloseEditPanelMethod, $intId=null, $strControlId = null) {
			// Call the Parent
			try {
				parent::__construct($objParentObject, $strControlId);
			} catch (QCallerException $objExc) {
				$objExc->IncrementOffset();
				throw $objExc;
			}
                        
                        $this->Template = __DOCROOT__ . __SAMPLES__ . '/tpl/SampleTitlesEditPanel.tpl.php';
			// Record Method Callbacks

			$this->strCloseEditPanelMethod = $strCloseEditPanelMethod;
                       
                        
                        $this->mctSampleTitles = SampleTitlesMetaControl::Create($this,$intId);

                        $this->txtaISBN = $this->mctSampleTitles->txtaISBN_Create();
                        $this->txtaISBN->Width = 130;

                        $this->txtaTitle = $this->mctSampleTitles->txtaTitle_Create();
                        $this->txtNote = $this->mctSampleTitles->txtNotes_Create();

                        $this->btnAdd = new QButton($this);
                        $this->btnAdd->Text = "Add";
                        $this->btnAdd->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnAdd_Click'));
			// Setup the Template

                        $this->lblError = new QLabel($this);
                        $this->lblError->CssClass= "Error";

                }



                public function btnAdd_Click($strFormId, $strControlId, $strParameter){
                    $this->btnAdd->Enabled = false;
                    sleep(0.5);
                    if ($_SESSION['SampleId'] == Null){
                        $this->lblError->Text = "Must Save Sample Header first";
                        $this->btnAdd->Enabled = true;
                    }
                    elseif ( strlen( $this->txtaISBN->Text) > 0 ){
                        $this->SaveSampleTitle();
                    }
                    elseif ( strlen( $this->txtaTitle->Text ) > 0) {
                         $array = Title::LoadArrayByTitle($this->txtaTitle->Text);
                         $carray = count($array);
                         if ( ( $carray > 1 ) || ($carray < 1) ){
                            $this->lblError->Text = "Cant get record. Please select ISBN";
                            $this->btnAdd->Enabled = true;
                         }
                         else{
                             $this->txtaISBN->Text = $array[0]->Isbn;
                             $this->SaveSampleTitle();
                         }
                      }
                      else{
                          $this->lblError->Text = "Must select a title";
                          $this->btnAdd->Enabled = true;
                      }


                }
                public function SaveSampleTitle(){
                        //Check Record is not dupplicate
                    /*
                        $array=SampleTitles::LoadArrayBySampleId($_SESSION['SampleId']);
                        $carray = count($array);
                        $dupplicate= null;
                        if ( $carray > 0)
                            foreach ($array as $title){
                                if ($title->Title->Isbn ==  $this->txtaISBN->Text ){
                                    $this->lblError->Text = "isbn";
                                    $dupplicate =true;
                                    break;
                                }
                            }

                        if ($dupplicate == true){
                             $this->btnAdd->Enabled = true;
                        }
                        else{
                     * 
                     */
                        $this->mctSampleTitles->SaveSampleTitles() ;
                        $this->ClearAll();
                        $this->btnAdd->Enabled = true; 
                        //}
                }

                public function ClearAll(){
                         echo "$this->strCloseEditPanelMethod";
                        //exit;
			$strMethod = $this->strCloseEditPanelMethod;
                        echo "$strMethod";
                        //exit;
			$this->objParentControl->ClosePanel("pnlSampleTitleEdit","dtgSampleTitles");
		

                }

	}
?>